// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.system.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.Menu;
import com.liuxinlong.modules.system.controller.request.QueryMenuReq;
import com.liuxinlong.modules.system.controller.request.UpdateMenuReq;
import com.liuxinlong.modules.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单管理控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022年5月12日
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    /**
     * 分页查询菜单类型信息
     *
     * @param req 请求信息
     * @return 菜单信息信息列表
     */
    @PostMapping("page")
    @LogAnnotation(value = "分页查询", business = LogBusinessConstants.MENU_MANAGE, operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp pageMenuInfo(@RequestBody QueryMenuReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("name", req.getName());
        queryMap.put("code", req.getCode());
        queryMap.put("parent", req.getParent());
        List<Map<String, Object>> dataList = menuService.pageMenuInfo(queryMap);
        int total = menuService.countMenu(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询菜单信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(total);
        return resp;
    }

    /**
     * 新增菜单类型信息信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("add")
    @LogAnnotation(value = "新增菜单信息", business = LogBusinessConstants.MENU_MANAGE, operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addMenuInfo(@RequestBody UpdateMenuReq req) {
        Menu menu = new Menu();
        menu.setName(req.getName());
        menu.setParentId(req.getParentId());
        menu.setCode(req.getCode());
        menu.setSort(req.getSort());
        menu.setRightType(req.getRightType());
        menu.setRightCode(req.getRightCode());
        menu.setUrl(req.getUrl());
        menu.setIcon(req.getIcon());
        menu.setRemark(req.getRemark());
        menuService.addMenuInfo(menu);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增菜单信息成功");
        return resp;
    }

    /**
     * 修改菜单类型信息信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("update")
    @LogAnnotation(value = "修改菜单信息", business = LogBusinessConstants.MENU_MANAGE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateMenuInfo(@RequestBody UpdateMenuReq req) {
        Menu menu = new Menu();
        menu.setId(req.getId());
        menu.setName(req.getName());
        menu.setParentId(req.getParentId());
        menu.setCode(req.getCode());
        menu.setSort(req.getSort());
        menu.setRightType(req.getRightType());
        menu.setRightCode(req.getRightCode());
        menu.setUrl(req.getUrl());
        menu.setIcon(req.getIcon());
        menu.setRemark(req.getRemark());
        menuService.updateMenuInfo(menu);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改菜单类型信息信息成功");
        return resp;
    }

    /**
     * 删除菜单类型信息信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value = "删除菜单信息", business = LogBusinessConstants.MENU_MANAGE, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteMenuInfo(@RequestBody BaseDeleteReq req) {
        menuService.deleteMenuInfo(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除菜单类型信息信息成功");
        return resp;
    }
//
//	/**
//	 * 树模糊查询菜单信息
//	 *
//	 * @param req 请求信息
//	 * @return 菜单展开列表
//	 */
//	@PostMapping("tree-query")
//	@LogAnnotation(value ="树模糊查询菜单信息",business = LogBusinessConstants.MENU_MANAGE)
//	public BaseResp treeQueryMenu(@RequestBody QueryMenuReq req) {
//		List<String> menuList = menuService.treeQueryMenu(req.getName());
//		Map<String,Object> dataMap = new HashMap<>();
//		dataMap.put("expandedKeys",menuList);
//		BaseResp resp = new BaseResp();
//		resp.setRetMessage("树模糊查询菜单信息成功");
//		resp.setDataMap(dataMap);
//		return resp;
//	}

    /**
     * 导出菜单信息列表
     *
     * @return 导出数据
     */
    @PostMapping("export")
    @LogAnnotation(value = "导出菜单信息列表", business = LogBusinessConstants.MENU_MANAGE, operateType = LogOperateConstants.OPERATE_EXPORT)
    public void exportMenuList(HttpServletResponse response) {
        menuService.exportMenuList(response);
    }

}
